# -*- coding:utf-8 -*-

from common.BaseFunction import BaseFunctions
from utils.Locator import Locator as loc
from utils.parseInfo import parseInfos
from utils.WirteToExcel import WriteToExcel
from utils.dynamicLoading import dynamicLoading
from utils.GetWorkspacePath import GetworkspacePath
import sys
from os import path
import tkinter as tk

def main(url):
    bf = BaseFunctions(url)
    driver = bf.getDriver("Firefox")
    bf.openURL(driver)
    wte = WriteToExcel()
    wte.openExcel()

    dynamicLoading(driver)

    elements = driver.find_elements_by_css_selector(loc.TITLE_CSS)
    for i, element in enumerate(elements):
        attr = bf.getAttr(element, "aria-label")
        infos = parseInfos(attr)
        wte.writeData(i, infos)

    wte.saveExcel()

    driver.quit()

def initialGUI():
    TITLE = "Youtube视频信息解析工具v1.0"
    WIDTH = 370
    HEIGHT = 30
    ICON = path.join(GetworkspacePath().CURRENT_DIR, "Youtube-ico.ico")
    window = tk.Tk()
    window.title(TITLE)
    window.iconbitmap(ICON)

    # Place Tools on the center of screen
    ws = window.winfo_screenwidth()
    hs = window.winfo_screenheight()
    x = (ws / 2) - (WIDTH / 2)
    y = (hs / 2) - (HEIGHT / 2)
    window.geometry('%dx%d+%d+%d' % (WIDTH, HEIGHT, x, y))

    # frame
    frame = tk.Frame(window)
    frame.pack(side=tk.TOP, fill=tk.X)
    # Input URL
    URL = tk.StringVar()
    tk.Label(frame,text="URL:", width=3).grid( column=0, row=1, pady=5, sticky="w")
    tk.Entry(frame, textvariable=URL, width=41, bd=2, justify=tk.RIGHT).grid(padx=4,column=1, row=1)
    tk.Button(frame, text="解析", width=3, height=1, command=lambda : main(URL.get())).grid(padx=4,column=2, row=1)



    window.mainloop()

if __name__ == '__main__':
    # args = sys.argv
    # url = args[1]
    url = r"https://www.youtube.com/channel/UCUGJ-yKqQHl4FSZwUmGpiUg/videos"
    main(url)
    # initialGUI()